package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.policeasset.mapper.ProjectBudgetMapper;
import com.zygh.policeasset.mapper.UploadFileMapper;
import com.zygh.policeasset.pojo.ProjectBudget;
import com.zygh.policeasset.pojo.TableType;
import com.zygh.policeasset.pojo.UploadFile;
import com.zygh.policeasset.pojo.vo.ProjectBudgetVO;
import com.zygh.policeasset.utils.AuditUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class ProjectBudgetService {
    @Autowired
    ProjectBudgetMapper projectBudgetMapper;

    @Autowired
    AuditFlowService auditFlowService;

    @Autowired
    UploadFileMapper uploadFileMapper;

    public PageInfo findAll(Integer pageNum, Integer pageSize, Integer status,
                            String projectNo, String time1, String time2, Integer userId) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(projectBudgetMapper.findAll(status, projectNo, time1, time2, userId));
    }

    @Transactional(rollbackFor = {Exception.class})
    public Integer add(ProjectBudget projectBudget) {

        try {
            String now = DateUtil.now();
            String flowNo = AuditUtil.getFlowNo(now);
            projectBudget.setFlowNo(flowNo);

            auditFlowService.add(projectBudget.getUserId(), flowNo, now, TableType.PROJECTBUDGET.getName(),
                    TableType.PROJECTBUDGET.getType(), projectBudget.getProjectNo(), null);
            Integer count = projectBudgetMapper.add(projectBudget);

            List<UploadFile> uploadFileList = projectBudget.getUploadFileList();

            if (uploadFileList.size() > 0) {
                for (int i = 0; i < uploadFileList.size(); i++) {
                    uploadFileList.get(i).setProjectId(projectBudget.getId());
                    uploadFileList.get(i).setProjectName("project_budget");
                    uploadFileList.get(i).setCreateDate(DateUtil.now());
                    this.uploadFileMapper.add(uploadFileList.get(i));
                }
            }
            return count;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return 0;
        }
    }

    public Integer update(ProjectBudget projectBudget) {
        return projectBudgetMapper.update(projectBudget);
    }

    public Integer delete(Integer id) {
        return projectBudgetMapper.delete(id);
    }

    public ProjectBudgetVO findById(Integer id) {
        return projectBudgetMapper.findById(id);
    }
}